#coding:utf-8
import os
from common.koufuApi import KoufuApi

__author__ = 'yangxianming'

from common.KoufuConf import KoufuConf
import json
import requests


def case():
    #初始化必要的参数
    kou_fu_conf = KoufuConf()
    test_url = kou_fu_conf.koufu_prod_domain + "/api/koufu"
    kou_fu_api = KoufuApi()
    script_name = os.path.basename(__file__)
    tf = True
    test_result = ""

    share_id = ""
    try:
        #创建拼单
        p = kou_fu_api.get_efuli_koufu_user_share_bill_create_data()
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个是接口efuli_koufu_user_share_bill_create返回的全部参数\n", r.content.decode("utf-8"))
        share_id = r.json()["return_data"]["shareId"]

        #我的拼单是否有记录
        p = kou_fu_api.get_efuli_koufu_user_share_bill_query_data(update_body={"isOwner":"Y"})
        r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
        print("这个是接口efuli_koufu_user_share_bill_query返回的全部参数\n", r.content.decode("utf-8"))

        test_result = r.json()
        first_share_id = test_result["return_data"]["list"][0]["shareId"]
        print("返回参数first_share_id值", first_share_id)
        #具体判断
        if  first_share_id != share_id:
            tf = False
    except Exception as e:
        err_info = str(e).replace("'", "").replace("\"", "")
        test_result = "非预期的异常,脚本文件:" + script_name + "; 错误信息：" + err_info
        tf = -2

    #调用拼单注销接口
    p = kou_fu_api.get_efuli_koufu_user_before_share_data(update_body={"shareId":share_id,"ignore":"Y"})
    r = requests.post(test_url, data=json.dumps(p), headers=kou_fu_conf.headers)
    print("这个是接口efuli_koufu_user_before_share返回的全部参数\n", r.content.decode("utf-8"))

    #定义预期描述
    print(str(tf))
    print(test_result)
    expect_result = "预期结果:通过查询我的拼单中第一条数据的share_id是否与创建的一致"

    #返回测试结果 第一个参数:True or False    第二个参数测试结果描述
    return tf, [test_result, expect_result]

# case()